package transaction.imp;

import classification.PaymentClassification;
import classification.imp.CommissionedClassification;
import dao.PayrollDatabase;
import dao.imp.GpayrollDatabase;
import domain.Employee;
import domain.SalesReceipt;
import exception.EmployeeException;
import transaction.Transaction;

import java.util.Date;

public class AddSalesReceiptTransaction implements Transaction {
    private int empid;
    private SalesReceipt salesReceipt;
    private PayrollDatabase payrollDatabase = new GpayrollDatabase();

    public AddSalesReceiptTransaction(int empid, Date date, double amount) {
        salesReceipt = new SalesReceipt(date, amount);
        this.empid = empid;
    }

    @Override
    public void execute() throws EmployeeException {
        Employee employee = payrollDatabase.getEmployee(empid);
        if(employee == null) {
            throw new EmployeeException("no employee " +empid);
        }

        PaymentClassification classification = employee.getClassification();
        if(classification instanceof CommissionedClassification) {
            CommissionedClassification commissionedClassification = (CommissionedClassification) classification;
            commissionedClassification.addSalesReceipt(salesReceipt);
        } else {
            throw new EmployeeException("the employee is not Commissioned employee, " + empid);
        }

    }
}
